#include <iostream>
using namespace std;
 
static int gEightQueen[12] = {0};
static int gCount = 0;
 
 
void print(int n)
{
	int outer;
	int inner;
 
	for(outer = 1; outer <=n; outer ++)
		
          printf("%d ",gEightQueen[outer]);
 
		printf("\n");
	
    
	printf("==========这是一个答案=============\n");
}
 
int check_pos_valid(int loop, int value)//输入新位置的坐标，loop=nx，value=ny
{
	int index;//行，
	int data;//列
 
	for(index = 1; index <= loop; index ++){
		data = gEightQueen[index];//第index行的值geightqueen就是它的列
        //表示在第index行的第data个
 
		if(value == data)//在同一列 ，拒绝
			return 0;
 
		if((index + data) == (loop + value))//在同一条副对角线上，拒绝
			return 0;
 
		if((index - data) == (loop - value))//在同一条主对角线上，拒绝
			return 0;
	}
 
	return 1;
}
 
 
 
void eight_queen(int index,int n)
{
	int loop;
 
	for(loop = 1; loop <= n; loop++){
		if(check_pos_valid(index, loop)){//判断（i，l）这个位置是否可以放置皇后
			gEightQueen[index] = loop;
 
			if(n== index){
				gCount ++, print(n);//找到一个解就输出一次
			    gEightQueen[index] = 0;
				return;
			}
			
			eight_queen(index + 1,n);//回溯
			gEightQueen[index] = 0;
		}
	}
}
 
 
 
int main(int argc, char* argv[])
{
    int n=0;
    scanf("%d",&n);
	eight_queen(1,n);
	printf("total = %d\n", gCount);
   
	return 1;
}